﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.ComponentModel.Composition.Extensions.Web;

namespace Steve.UI.Site
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple=false, Inherited=true)]
    public sealed class SiteMasterDataAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var account = MefHttpApplication.ScopeManager.GetExportedValue<IAccountService>();
            var viewData = filterContext.Controller.ViewData;

            viewData["user"] = account.GetUser();
            viewData["authenticatedUser"] = account.GetAuthenticatedUser();
        }

        internal static void ClearUser(ViewDataDictionary viewData)
        {
            viewData.Remove("user");
            viewData.Remove("authenticatedUser");
        }
    }
}